package com.longg.dao;

import com.longg.dao.base.BaseDao;
import com.longg.model.UserInfo;
import com.longg.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

public class UserInfoDao extends BaseDao<UserInfo> {
    public UserInfoDao() {
        super(UserInfo.class);
    }


    public long getCount(String name) {
        String where = "";
        if(null != name && !"".equals(name)){
            where = " where real_name like ? ";
        }
        String sql = "select count(*) from user_info" + where;
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        Long query = 0l;
        try {
            if(null != name && !"".equals(name)){
                query = queryRunner.query(sql, new ScalarHandler<Long>(),"%" + name + "%");
            }else {
                query = queryRunner.query(sql, new ScalarHandler<Long>());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

}
